package com.better.backend.mqReceiverService.studentRecever.listener;

import com.alibaba.fastjson.JSONObject;
import com.better.backend.mqReceiverService.studentRecever.model.ScoreReceverModel;
import com.better.backend.mqReceiverService.studentRecever.repository.ScoreReceverModelRepository;
import com.better.common.mq.MqTopic;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xiaq
 * 消息接收
 * @create 2017-09-23 12:01
 **/
@Component
public class ScoreReceiver {
    @Autowired
    private ScoreReceverModelRepository scoreReceverModelRepository;
    private static Log log = LogFactory.getLog(ScoreReceiver.class);
    @RabbitListener(queues = MqTopic.ZXY_ADDSCORE)
    public void process(Message examModel) {
        String a = new String(examModel.getBody());
        log.info("接收添加考试消息：topic==zxy_addScore。"+a);
        ScoreReceverModel scoreReceverModel = JSONObject.parseObject(a, ScoreReceverModel.class);
        //删除已经进行同步的成绩数据
        this.scoreReceverModelRepository.deleteScoreReceverModelByClassIdAndExamIdAndSubjectId(scoreReceverModel.getClassId(),scoreReceverModel.getExamId(),scoreReceverModel.getSubjectId());
        this.scoreReceverModelRepository.save(scoreReceverModel);
    }
}
